<template>
  <div class="material-release">
    <div class="content-operation">
      <div @click="closePop">取消</div>
      <div class="click-release" @click="onSubmit">发表</div>
    </div>
    <div class="set-content">
      <textarea v-model="formData.content" maxlength="200" class="textarea" placeholder="请添加描述，最多添加200字"></textarea>
    </div>
    <div class="upload-imgs">
      <!-- <van-uploader :max-count="9" :after-read="afterRead" v-model="fileList" @delete="deleteImg" /> -->
      <SmallUploadImg
        ref="smallUploadImg"
        :maxLength="9"
        @changeImgList="changeImgList"
        :isChangeStyle="true"
      ></SmallUploadImg>
    </div>
    <div class="other">
      <div @click="labelPop = true, num++">
        <div class="left"><img class="icon" src="https://img.wifenxiao.com/h5-wfx/images/materialCenter/lable-icon.png">添加标签</div>
        <div class="tips-label" v-if="formData.label.length > 0">- 标签：{{formData.label.join(', ')}}</div>
      </div>
      <div v-if="allCate.length > 0" @click="catePop = true, num++">
        <div class="left"><img class="icon" src="https://img.wifenxiao.com/h5-wfx/images/materialCenter/cate-icon.png">选择分类</div>
        <div class="tips-label" v-if="formData.category_ids.length > 0">- 分类：{{categoryList.map(item => item.name).join(', ')}}</div>
      </div>
      <div @click="goodsPop = true, num++">
        <div class="left"><img class="icon" src="https://img.wifenxiao.com/h5-wfx/images/materialCenter/content-icon.png">选择商品</div>
        <img class="tips-img" v-if="goodsContent.pic_url" :src="goodsContent.pic_url">
        <div class="tips-label" v-if="goodsContent.title">- 商品：{{goodsContent.title}}</div>
      </div>
    </div>
    <div class="reset" @click="onReset">
      <img class="icon" src="https://img.wifenxiao.com/h5-wfx/images/materialCenter/reset.png">一键清空
    </div>
    <van-popup v-model="labelPop" class="label-wrap" position="bottom" :style="{ height: '100%', width: '100%' }" @close="upPop = false">
      <add-label @success="closeLabelPop" :num="num"  :labelBefore="formData.label"></add-label>
    </van-popup>
    <van-popup v-model="catePop" position="bottom" :style="{ height: '100%', width: '100%' }" @close="catePop = false">
      <all-cate @success="closeCatePop" :number="num" :categoryPopup="catePop" :category_ids="formData.category_ids"></all-cate>
    </van-popup>
    <van-popup v-model="goodsPop" position="bottom" :style="{ height: '100%', width: '100%' }" @close="catePop = false">
      <all-goods @success="closeGoodsPop" :num="num" :goodsPop="goodsPop" :item_id="formData.item_id"></all-goods>
    </van-popup>
  </div>
</template>
<script>
import addLabel from './addLabel'
import allCate from './allCate'
import allGoods from './allGoods'
import { getAllCate, gysAdd } from '@/api/community/materialCenter'
import SmallUploadImg from "@/components/SmallUploadImg/index.vue";

export default {
  components: {
    addLabel,
    allCate,
    allGoods,
    SmallUploadImg
  },
  data() {
    return {
      fileList: [],
      formData: {
        content: '',
        item_id: '',
        images: [],
        label: [],
        category_ids: []
      },
      labelPop: false,
      catePop: false,
      goodsPop: false,
      num: 0,
      allCate: [],
      categoryList: [],
      goodsContent: {}
    }
  },
  created() {
    this.cateLists()
  },
  methods: {
    cateLists() {
      getAllCate({ gys_add: 1, name: '' }).then(res => {
        this.allCate = res.data
      })
    },
    closePop() {
      this.$emit('success', 'close')
    },
    closeLabelPop(data) {
      if (data == 'close') {
        this.labelPop = false
      } else {
        this.formData.label = data
        this.labelPop = false
      }
    },
    closeCatePop(data) {
      if (data == 'close') {
        this.catePop = false
      } else {
        this.formData.category_ids = data
        const result = this.allCate.filter(item => data.includes(item.id))
        this.categoryList = result
        this.catePop = false
      }
    },
    closeGoodsPop(data) {
      if (data == 'close') {
        this.goodsPop = false
      } else {
        this.formData.item_id = data.item_id
        this.goodsContent = data
        this.goodsPop = false
      }
    },
    changeImgList(imgList) {
      // console.log("imgList", imgList);
      this.formData.images = imgList;
    },
    onReset() {
      this.formData = {
        content: '',
        item_id: '',
        images: [],
        label: [],
        category_ids: []
      }
      this.fileList = []
      this.categoryList = []
      this.goodsContent = {}
    },
    deleteImg(file, e) {
      const idx = e.index
      const arr = [...this.fileList]
      arr.splice(idx, 1)
      this.handleDealData(arr)
    },
    handleDealData(files) {
      const arr = []
      files.forEach(item => {
        arr.push(item.content)
      })
      this.formData.images = arr
    },
    onSubmit() {
      if (this.formData.content == '' && this.formData.images.length == 0) {
        this.$Error('请输入素材描述或上传图片')
        return
      }
      this.$loadingWrap.show()
      gysAdd(this.formData).then(res => {
        this.$loadingWrap.close()
        if (res.status == 1) {
          this.$Error('发布成功')
          this.onReset()
          this.$emit('success', 'success')
        } else {
          this.$Error(res.msg)
        }
      })
    }
  }
}
</script>
<style lang="scss">
.upload-imgs {
  margin-bottom: 60px;
  .samll-upload-box-item {
    width: 200px!important;
    height: 200px!important;
  }

  .samll-upload .uploadImage {
    width: 200px!important;
    height: 200px!important;
  }
  .samll-upload-box-wrap {
    width: 200px!important;
    height: 200px!important;
  }
}
.material-release {
  padding: 84px 44px 44px;
  .content-operation {
    background: #fff;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 28px;
    position: fixed;
    top: 0px;
    left: 0;
    right: 0;
    z-index: 10;
    padding: 24px 24px 16px 24px;
    height: 84px;
    // font-weight: 500;
    .click-release {
      background: #FF2A40;
      color: #fff;
      padding: 16px 20px;
      border-radius: 8px;
      font-weight: 400;
    }
  }
  .set-content {
    margin: 20px;
    textarea {
      width: 100%;
      height: 360px;
      size-adjust: none;
      line-height: 40px;
    }
  }
}
.other {
  font-weight: 500;
  font-size: 28px;
  border-bottom: 2px solid #f1f1f1;

  >div {
    border-top: 2px solid #f1f1f1;
    padding: 26px 0;
    background: url("https://img.wifenxiao.com/h5-wfx/images/sideWidget/icon_addrarr.png") no-repeat right center;
    background-size: 14px auto;
    position: relative;
      .left {
        display: flex;
        align-items: center;
        .icon {
          width: 40px;
          height: 40px;
          margin-right: 30px;
        }
      }
  }
  .tips-label {
    font-size: 22px;
    color: #999999;
    margin-left: 60px;
    margin-top: 10px;
    line-height: 40px;
    word-break: break-all;
    padding-right: 40px;
  }
  .tips-img {
    width: 40px;
    height: 40px;
    position: absolute;
    right: 30px;
    top: 50%;
    margin-top: -20px;
  }
}
.label-wrap {
  background: #f1f1f1;
}
.reset {
  border: 2px solid #999;
  padding: 0px 20px;
  width: fit-content;
  border-radius: 40px;
  margin-top: 40px;
  color: #999;
  float: right;
  line-height: 60px;
  font-size: 26px;
  img {
    width: 30px;
    height: 30px;
    margin-right: 10px;
    position: relative;
    top: 4px;
  }
}
</style>